﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data.Linq;
using System.Linq;
using System.Text;
using IntraVision.Web;
using IntraVision.Web.Filter;
using IntraVision.Web.Security;
using Model;

[AllowedRole("IsAdmin")]
public class EventLogController : TableController<NotarialServiceDataContext, EventLog>
{
    public EventLogController() { }

    public override void Default()
    {
        this.List();
    }

    public override void Create()
    {
        return;
    }

    public override void Delete()
    {
        return;
    }

    public override void Save()
    {
        return;
    }

    protected override IQueryable ConvertQueryToResult(NotarialServiceDataContext dc, IQueryable<EventLog> query)
    {
        return (from l in query
        select new
        {
            Date = l.Date,
            Ip = l.Ip,
            Login = (l.UserId == null) ? l.Person.Email : l.User.Login,
            UserName = (l.UserId == null) ? "" : l.User.LastName + " " + l.User.Name + " " + l.User.PatronymicName,
            ClientName = ((l.PersonId != null) ? (l.Person.LastName + " " + l.Person.Name + " " + l.Person.PatronymicName) : ""),
            Operation = l.EventLogOperation.Name
        });
    }

    protected override void CreateFilterConditions(NotarialServiceDataContext dc)
    {
        Filter = new Filter<EventLog>();
        Filter.Conditions.Add(new DateFilterCondition<EventLog>("LoginDate", "Дата/время операции"));
        Filter.Conditions.Add(new StringFilterCondition<EventLog>("Ip", "IP-адрес"));
        Filter.Conditions.Add(new StringFilterCondition<EventLog>("Login", "Логин"));
        Filter.Conditions.Add(new UserSelectFilterCondition<EventLog>("UserId", "Сотрудник", (from u in dc.Users orderby u.LastName select new { Id = u.Id, Name = u.FullName }), -1, "task.ivp/findusers", "user.ivp/select?reset=true"));
        Filter.Conditions.Add(new UserSelectFilterCondition<EventLog>("PersonId", "Клиент", (from p in dc.Persons orderby p.LastName select new { Id = p.Id, Name = p.FIO }), -1, "task.ivp/findpersons", "person.ivp/select?reset=true"));
    }
}


